var member;
$(function(){
	member=getEntity("memberInfo");
	init();
})

function setDefaultAddress(maId){
	$.ajax({
		type:'post',
		url:rootPath+'/address/setDefaultAddress',
		data:{'maId':maId},
		success:function(data){
			if(data.resultDesc){
				init();
			}
		}
	})
}

function moAdress(maId){
	window.location.href=rootPath+"/address/updateAddress?maId="+maId;
}
function init(){
	$.ajax({
		type:'post',
		url:rootPath+'/address/selectBymId',
		data:{'mId':member.mId},
		success:function(data){
			if(data.resultDesc){
				var addressList=data.data;
				//console.log(addressList);
				var str='';
				str=createHtml(addressList);
				//console.log(str);
				$("#mAdress").empty();
				$("#mAdress").append(str);
			}
		}
	})
}
function createHtml(addressList){
	var str='';
	var imgDefault='';
	var isDefault='';
	$.each(addressList,function(i,item){
		if(item.isdefault=="0"){
			imgDefault=rootPath+'/image/agree1.png';
			isDefault='';
		}else{
			imgDefault=rootPath+'/image/agree.png';
			isDefault='<p>默认</p>';
		}
		str+='<div class=" clearfix selectAdres maAdress"><ul class="maAdd"><li>'
			+'<p>'+item.name+'</p>'+isDefault+'<span class="fr">'+item.tel+'</span></li>'
			+'<li class="wang"> <p>'+item.streetAll+'</p></li></ul>'
			+'<div class="selectA"><img onclick="setDefaultAddress(\''+item.maId+'\')" src="'+imgDefault+'" alt="" class="fl selectImg"/><span class="fl">默认地址</span>'
			+'<a href="#" onclick="deleteAddress(\''+item.maId+'\')"><span class="fr selectM">删除</span><img src="'+rootPath+'/image/dl1.png" alt="" class="fr"/></a>'
			+'<a href="javascript:void(0);"  onclick="moAdress(\''+item.maId+'\');"><span class="fr" >修改</span><img src="'+rootPath+'/image/mod.png" alt="" class="fr"/></a></div></div>';
	})
	return str;
}

/**优化删除，删除时给入提示：是否确定删除，删除后需要重新添加*/
function deleteAddress(maId){
	if(confirm("确定要删除地址吗？")){
		$.ajax({
			type:'post',
			url:rootPath+'/address/deleteAddressById',
			data:{'maId':maId},
			success:function(data){
				if(data.resultDesc){
					location.reload(true);
				}
			}
		});
	}else{
		return false;
	}
}
